package com.lsj.config.security;

import com.lsj.config.security.filter.JwtAuthenticationFilter;
import com.lsj.config.security.handler.MyAccessDeinedHandler;
import com.lsj.config.security.handler.MyAuthenticationEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @Author:lin.sj
 * @Data:2021/7/5 23:02
 * @Desc:
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    UserDetailsService userDetailsService;

//    @Autowired
//    MyAuthenticationSuccessHandler mySuccessHandler;
//    @Autowired
//    MyAuthenticationFailureHandler myFailureHandler;
    @Autowired
    JwtAuthenticationFilter jwtAuthenticationFilter;
    @Autowired
    MyAccessDeinedHandler myAccessDeinedHandler;
    @Autowired
    MyAuthenticationEntryPoint myAuthenticationEntryPoint;
    @Autowired
    SecurityProp securityProp;

    @Override
    @Bean
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        String[] ignoresArray = securityProp.getIgnoreResources()
                .toArray(new String[securityProp.getIgnoreResources().size()]);
        //将项目中静态资源路径开放出来
        web.ignoring().antMatchers(ignoresArray);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //获取需要的url数组
        String[] ignoresArray = securityProp.getIgnoreUrls()
                .toArray(new String[securityProp.getIgnoreUrls().size()]);
        //关闭csrf
        http.csrf().disable();
        //开启cors跨域访问
        http.cors();
        //设置访问请求
        http.authorizeRequests()
                //放行登录所需的请求
                .antMatchers(ignoresArray).permitAll()
                //其余请求需要根据鉴权规则进行设置
                .anyRequest().access("@rbacService.hasPermission(request,authentication)");
        //设置权限异常处理
        http.exceptionHandling()
                //权限不足处理
                .accessDeniedHandler(myAccessDeinedHandler)
                //未登录时处理
                .authenticationEntryPoint(myAuthenticationEntryPoint);
        //前后端应用，禁止使用session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        //增加jwt认证过滤器
        http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }
}
